package com.rms.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.rms.domain.Job;
import com.rms.domain.Resume;
import com.rms.domain.Worker;
import com.rms.utils.servlet.BaseServlet;

/**Worker及各种功能:
 * 登录、注册、退出
 * @author Zheng weiyu
 * @date 2019年7月29日
 */
@WebServlet("/WorkerServlet")
public class WorkerServlet extends BaseServlet {
	public static final String SESSION_NAME = "worker";
	/**
	 * 封装表单数据
	 * 
	 * @param req
	 * @return
	 * @author Zheng weiyu 2019年7月29日
	 */
	private Worker toBean(HttpServletRequest req) {
		Worker worker = new Worker();
		try {
			BeanUtils.populate(worker, req.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return worker;
	}
//在baseServlet中实现了
//	public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		/**
//		 * 1.得到表单数据，封装成javaBean 
//		 * 2.调用service的login方法 
//		 * 3.登陆成功,保存用户到session域，转发到main.jsp 登录失败转发到login.jsp
//		 */
//		// 封装表单数据
//		Worker worker = toBean(req);
//		// 调用service的login方法
//		try {
//			Worker w = workerService.login(worker,Worker.class);
//			req.getSession().setAttribute(SESSION_NAME, w);
//			// 执行成功,跳转到main.jsp
//			req.getRequestDispatcher("main.jsp").forward(req, resp);
//		} catch (Exception e) {
//			// 登录失败，保存错误信息并跳转到login.jsp
//			e.printStackTrace();
//			req.setAttribute("message", "登录失败!" + e.getMessage());
//			req.getRequestDispatcher("login.jsp").forward(req, resp);
//		}
//	}

	public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.封装表单数据 2.调用service的regist方法 3.注册成功转发到login.jsp 注册失败转发到regist.jsp
		 */
		// 封装表单数据
		Worker worker = toBean(req);
		// 调用service的login方法
		try {
			workerService.regist(worker);
			// 执行成功,跳转到main.jsp
			req.setAttribute("message", "注册成功!");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// 登录失败，保存错误信息并跳转到login.jsp
			e.printStackTrace();
			req.setAttribute("message", "注册失败!" + e.getMessage());
			req.getRequestDispatcher("regist.jsp").forward(req, resp);
		}
	}

	public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.移除session域的user 2.成功转发到login.jsp
		 */
		try {
			req.getSession().removeAttribute(SESSION_NAME);
			req.setAttribute("message", "退出成功!");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**投递简历
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 * @author Zheng weiyu
	 * 2019年7月31日
	 */
	public void sendResumeToCompany(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 1.得到简历id
		 * 2.得到工作id
		 * 3.封装到resumeJob实体类
		 * 4.调用service中的sendResumToCompany方法完成简历投递
		 * */
		//得到worker
		Worker worker = (Worker) req.getSession().getAttribute("worker");
		//得到worker的resume
		Resume resume = worker.getResume();
//		System.out.println(resume);
		int job_id = Integer.parseInt(req.getParameter("job_id"));
		try {
			workerService.sendResumToCompany(resume,job_id);
			req.setAttribute("message", "投递成功!");
			req.getRequestDispatcher("ToWorkMainPageServlet").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("message", e.getMessage());
			req.getRequestDispatcher("ToWorkMainPageServlet").forward(req, resp);
		}
	}
	
	/**上传简历
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 * @author Zheng weiyu
	 * 2019年7月29日
	 */
	public void uploadResume(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 调用service的uploadResume方法
		 * */
		try {
			workerService.uploadResume(req);
			req.setAttribute("message", "上传成功");
			req.getRequestDispatcher("ToWorkMainPageServlet").forward(req, resp);
		} catch (Exception e) {
			//上传失败
			e.printStackTrace();
			req.setAttribute("message", e.getMessage());
			req.getRequestDispatcher("resumeUpload.jsp").forward(req, resp);
		}
	}
}
